//
//  FMDBSupplierManager.m
//  takeout
//
//  Created by Mac on 12-3-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FMDBSupplierManager.h"
#import "FMDBManager.h"
#import "FMDatabase.h"
#import "Supplier.h"
@implementation FMDBSupplierManager
//插入
-(void) insert:(FMDatabase *)db supplier:(Supplier *)supplier{
    [db executeUpdate:@"INSERT INTO suppliers(id,name,address,map_address,longtitude,latitude,phone,jointime,starttime,endtime,leastprice,leastdistance,deliverydesc,avgprice,consumerecord,picture,desc,sup_cate_id,city,phone2,telephone,telephone2,road,suppliercategoryname) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[NSNumber numberWithInt:[supplier supplierId]],[supplier supplierName],[supplier address],[supplier mapAddress],[NSNumber numberWithFloat:[supplier longitude]],[NSNumber numberWithFloat:[supplier latitude]],[supplier phone],[supplier joinTime],[NSNumber numberWithInt:[supplier startTime]],[NSNumber numberWithInt:[supplier endTime]],[NSNumber numberWithFloat:[supplier leastPrice]],[NSNumber numberWithFloat:[supplier leastDistance]],[supplier deliveryDesc],[NSNumber numberWithFloat:[supplier avgPrice]],[NSNumber numberWithInt:[supplier consumerecord]],[supplier imgUrl],[supplier desc],[NSNumber numberWithInt:[supplier supCateId]],[NSNumber numberWithInt:[supplier city]],[supplier phone2],[supplier telephone],[supplier telephone2],[supplier road],[supplier supplierCategoryName]];
}

//查询
-(void) select{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM suppliers"];
    while ([rs next]){
        NSLog(@"id%@ name%@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@",[rs stringForColumn:@"id"],[rs stringForColumn:@"name"],[rs stringForColumn:@"address"],[rs stringForColumn:@"map_address"],[rs stringForColumn:@"longtitude"],[rs stringForColumn:@"latitude"],[rs stringForColumn:@"phone"],[rs stringForColumn:@"jointime"],[rs stringForColumn:@"starttime"],[rs stringForColumn:@"endtime"],[rs stringForColumn:@"leastprice"],[rs stringForColumn:@"leastdistance"],[rs stringForColumn:@"deliverydesc"],[rs stringForColumn:@"avgprice"],[rs stringForColumn:@"consumerecord"],[rs stringForColumn:@"picture"],[rs stringForColumn:@"desc"],[rs stringForColumn:@"sup_cate_id"],[rs stringForColumn:@"city"],[rs stringForColumn:@"phone2"],[rs stringForColumn:@"telephone"],[rs stringForColumn:@"telephone2"],[rs stringForColumn:@"road"],[rs stringForColumn:@"suppliercategoryname"]);
    }
    [rs close];
//    [fmdbManager closeDataBase];
}
//删除
-(void)deleteBySupplierId:(NSString *)supplierId{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    [db executeUpdate:@"DELETE FROM suppliers where id=?",supplierId];
//    [fmdbManager closeDataBase];
}

//获取本地商家列表数据
-(NSArray *) getSupplierByCategoryIdAndPageFromLoc:(NSString *)supplierCategoryId sPage:(NSString *)sPage road:(NSString *) road isSelling:(NSString *) isSelling{
    NSDate * startDate = [[NSDate alloc] init];
    NSCalendar * chineseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | 
    NSSecondCalendarUnit | NSDayCalendarUnit  | 
    NSMonthCalendarUnit | NSYearCalendarUnit;
    NSDateComponents * cps = [chineseCalendar components:unitFlags fromDate:startDate];
    NSInteger hour = [cps hour];
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    NSString *URIString=[NSString stringWithFormat:@"SELECT * FROM suppliers where 1=1"];
    if(supplierCategoryId!=nil){
        URIString = [URIString stringByAppendingFormat:@" and sup_cate_id=%@",supplierCategoryId]; 
    }
    if(road!=nil){
        URIString = [URIString stringByAppendingFormat:@" and road='%@'",road]; 
    }
    FMResultSet *rs = [db executeQuery:URIString]; 
    NSMutableArray *allSuppliers=[NSMutableArray arrayWithCapacity:8];
    
    
    while ([rs next]) {
        tempSupplier=[[Supplier alloc]init];
        tempSupplier.supplierId=[rs intForColumn:@"id"];
        tempSupplier.supplierName=[rs stringForColumn:@"name"];
        tempSupplier.address=[rs stringForColumn:@"address"];
        tempSupplier.mapAddress=[rs stringForColumn:@"map_address"];
        tempSupplier.longitude=[rs doubleForColumn:@"longtitude"];
        tempSupplier.latitude=[rs doubleForColumn:@"latitude"];
        tempSupplier.phone=[rs stringForColumn:@"phone"];
        tempSupplier.joinTime=[rs stringForColumn:@"jointime"];
        tempSupplier.startTime=[rs intForColumn:@"starttime"];
        tempSupplier.endTime=[rs intForColumn:@"endtime"];
        tempSupplier.leastPrice=[rs doubleForColumn:@"leastprice"];
        tempSupplier.leastDistance=[rs doubleForColumn:@"leastdistance"];
        tempSupplier.deliveryDesc=[rs stringForColumn:@"deliverydesc"];
        tempSupplier.avgPrice=[rs doubleForColumn:@"avgprice"];
        tempSupplier.consumerecord=[rs intForColumn:@"consumerecord"];
        tempSupplier.imgUrl=[rs stringForColumn:@"picture"];
        tempSupplier.desc=[rs stringForColumn:@"desc"];
        tempSupplier.supCateId=[rs intForColumn:@"sup_cate_id"];
        tempSupplier.city=[rs intForColumn:@"city"];
        tempSupplier.phone2=[rs stringForColumn:@"phone2"];
        tempSupplier.telephone=[rs stringForColumn:@"telephone"];
        tempSupplier.telephone2=[rs stringForColumn:@"telephone2"];
        tempSupplier.road=[rs stringForColumn:@"road"];
        tempSupplier.supplierCategoryName=[rs stringForColumn:@"suppliercategoryname"];
        if(isSelling!=nil){
            if(tempSupplier.startTime>tempSupplier.endTime){  //隔夜
                if(isSelling==@"y"){
                    if(hour>=tempSupplier.startTime||hour<tempSupplier.endTime){
                        [allSuppliers addObject:tempSupplier];
                    }
                }
                if(isSelling==@"n"){
                    if(hour<tempSupplier.startTime&&hour>=tempSupplier.endTime){
                        [allSuppliers addObject:tempSupplier];
                    }
                }
            }
            else{    //正常营业
                if(isSelling==@"y"){
                    if(hour>=tempSupplier.startTime&&hour<tempSupplier.endTime){
                        [allSuppliers addObject:tempSupplier];
                    }
                }
                if(isSelling==@"n"){
                    if(hour<tempSupplier.startTime||hour>=tempSupplier.endTime){
                        [allSuppliers addObject:tempSupplier];
                    }
                }
            }
            continue;
        }
        [allSuppliers addObject:tempSupplier];

    }
    [rs close];
//    [fmdbManager closeDataBase];
    NSInteger newPage=sPage.integerValue;
    NSMutableArray *suppliers=[NSMutableArray arrayWithCapacity:5];
    for(int i=5*(newPage-1);i<5*newPage;i++){
        if(i<allSuppliers.count){
            [suppliers addObject:[allSuppliers objectAtIndex:i]];
        }
    }
    return suppliers;
}

//根据商铺类型id插入商铺列表到本地
-(void) insertSupplierByCategoryId:(NSArray *)suppliers supplierCategoryId:(NSString *)supplierCategoryId sPage:(NSString *)sPage{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    for(int i=0;i<[suppliers count];i++){
        Supplier *supplier=[suppliers objectAtIndex:i];
        NSString *supplieridToString = [NSString stringWithFormat:@"%d",[supplier supplierId]];
        [self deleteBySupplierId:supplieridToString];
        [self insert:db supplier:supplier];
    }
//    [fmdbManager closeDataBase];
}

//获取本地商家详细信息
-(Supplier *) getSupplierDetailByIdFromLoc:(NSString *)suppierId{
    newSupplier=[[Supplier alloc]init];
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM suppliers where id=?",suppierId];
    while ([rs next]) {
        newSupplier.supplierId=[rs intForColumn:@"id"];;
        newSupplier.supplierName=[rs stringForColumn:@"name"];
        newSupplier.address=[rs stringForColumn:@"address"];
        newSupplier.mapAddress=[rs stringForColumn:@"map_address"];
        newSupplier.longitude=[rs doubleForColumn:@"longtitude"];
        newSupplier.latitude=[rs doubleForColumn:@"latitude"];
        newSupplier.phone=[rs stringForColumn:@"phone"];
        newSupplier.joinTime=[rs stringForColumn:@"jointime"];
        newSupplier.startTime=[rs intForColumn:@"starttime"];
        newSupplier.endTime=[rs intForColumn:@"endtime"];
        newSupplier.leastPrice=[rs doubleForColumn:@"leastprice"];
        newSupplier.leastDistance=[rs doubleForColumn:@"leastdistance"];
        newSupplier.deliveryDesc=[rs stringForColumn:@"deliverydesc"];
        newSupplier.avgPrice=[rs doubleForColumn:@"avgprice"];
        newSupplier.consumerecord=[rs intForColumn:@"consumerecord"];
        newSupplier.imgUrl=[rs stringForColumn:@"picture"];
        newSupplier.desc=[rs stringForColumn:@"desc"];
        newSupplier.supCateId=[rs intForColumn:@"sup_cate_id"];
        newSupplier.city=[rs intForColumn:@"city"];
        newSupplier.phone2=[rs stringForColumn:@"phone2"];
        newSupplier.telephone=[rs stringForColumn:@"telephone"];
        newSupplier.telephone2=[rs stringForColumn:@"telephone2"];
        newSupplier.road=[rs stringForColumn:@"road"];
        newSupplier.supplierCategoryName=[rs stringForColumn:@"suppliercategoryname"];
    }
    [rs close];
//    [fmdbManager closeDataBase];
    
    return newSupplier;
}

//根据菜品或商家名称查找数据
-(NSArray *) getSuppliersByNameOrDishAndPage:(NSString *)conditions sPage:(NSString *)sPage{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    NSString *uriString = [NSString stringWithFormat:@"SELECT * FROM dish_category AS a, dish AS b, suppliers c WHERE (c.name LIKE '%%%@%%' OR b.name LIKE '%%%@%%') AND a.id = b.dish_cate_id AND a.supplierid = c.id GROUP BY c.name limit(%@-1)*5,5",conditions,conditions,sPage];
    FMResultSet *rs=[db executeQuery:uriString];
    NSMutableArray *suppliers=[NSMutableArray arrayWithCapacity:2];
    while ([rs next]) {
        Supplier *supplier=[[Supplier alloc]init];
        supplier.supplierId=[rs intForColumn:@"id"];;
        supplier.supplierName=[rs stringForColumn:@"name"];
        supplier.address=[rs stringForColumn:@"address"];
        supplier.mapAddress=[rs stringForColumn:@"map_address"];
        supplier.longitude=[rs doubleForColumn:@"longtitude"];
        supplier.latitude=[rs doubleForColumn:@"latitude"];
        supplier.phone=[rs stringForColumn:@"phone"];
        supplier.joinTime=[rs stringForColumn:@"jointime"];
        supplier.startTime=[rs intForColumn:@"starttime"];
        supplier.endTime=[rs intForColumn:@"endtime"];
        supplier.leastPrice=[rs doubleForColumn:@"leastprice"];
        supplier.leastDistance=[rs doubleForColumn:@"leastdistance"];
        supplier.deliveryDesc=[rs stringForColumn:@"deliverydesc"];
        supplier.avgPrice=[rs doubleForColumn:@"avgprice"];
        supplier.consumerecord=[rs intForColumn:@"consumerecord"];
        supplier.imgUrl=[rs stringForColumn:@"picture"];
        supplier.desc=[rs stringForColumn:@"desc"];
        supplier.supCateId=[rs intForColumn:@"sup_cate_id"];
        supplier.city=[rs intForColumn:@"city"];
        supplier.phone2=[rs stringForColumn:@"phone2"];
        supplier.telephone=[rs stringForColumn:@"telephone"];
        supplier.telephone2=[rs stringForColumn:@"telephone2"];
        supplier.road=[rs stringForColumn:@"road"];
        supplier.supplierCategoryName=[rs stringForColumn:@"suppliercategoryname"];
        [suppliers addObject:supplier];
    }
    [rs close];
//    [fmdbManager closeDataBase];
    return suppliers;
}

@end
